package org.inh.ld20.dangerousbrother;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

public class Art {
    public static BufferedImage titleScreen = load("/title.png");
    public static BufferedImage level = load("/level.png");
    
    public static BufferedImage tile_grass = load("/grass.png");
    public static BufferedImage tile_sky = load("/sky.png");
    public static BufferedImage tile_underground = load("/underground.png");
    
    public static Animation mainCharacter = new Animation(
    		new BufferedImage[] {
    				Art.load("/Fred_Stand_Normal.png"),
    				Art.load("/Fred_Walk_Normal.png")
    		}, 5
    		);
    
    public static Animation follower1 = new Animation(
    		new BufferedImage[] {
    			Art.load("/follower1_stand.png"),
    			Art.load("/follower1_walk.png")
    		}, 6.2
    		);
    public static Animation follower2 = new Animation(
    		new BufferedImage[] {
    			Art.load("/follower2_stand.png"),
    			Art.load("/follower2_walk.png")
    		}, 6.6
    		);
    

    public static BufferedImage load(String name) {
        try {
            BufferedImage org = ImageIO.read(Art.class.getResource(name));
            BufferedImage res = new BufferedImage(org.getWidth(), org.getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics g = res.getGraphics();
            g.drawImage(org, 0, 0, null, null);
            g.dispose();
            return res;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static BufferedImage scale(BufferedImage src, int scale) {
        int w = src.getWidth() * scale;
        int h = src.getHeight() * scale;
        BufferedImage res = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        Graphics g = res.getGraphics();
        g.drawImage(src.getScaledInstance(w, h, Image.SCALE_AREA_AVERAGING), 0, 0, null);
        g.dispose();
        return res;
    }

    private static BufferedImage[][] mirrorsplit(BufferedImage src, int xs, int ys) {
        int xSlices = src.getWidth() / xs;
        int ySlices = src.getHeight() / ys;
        BufferedImage[][] res = new BufferedImage[xSlices][ySlices];
        for (int x = 0; x < xSlices; x++) {
            for (int y = 0; y < ySlices; y++) {
                res[x][y] = new BufferedImage(xs, ys, BufferedImage.TYPE_INT_ARGB);
                Graphics g = res[x][y].getGraphics();
                g.drawImage(src, xs, 0, 0, ys, x * xs, y * ys, (x + 1) * xs, (y + 1) * ys, null);
                g.dispose();
            }
        }
        return res;
    }

    private static BufferedImage[][] split(BufferedImage src, int xs, int ys) {
        int xSlices = src.getWidth() / xs;
        int ySlices = src.getHeight() / ys;
        BufferedImage[][] res = new BufferedImage[xSlices][ySlices];
        for (int x = 0; x < xSlices; x++) {
            for (int y = 0; y < ySlices; y++) {
                res[x][y] = new BufferedImage(xs, ys, BufferedImage.TYPE_INT_ARGB);
                Graphics g = res[x][y].getGraphics();
                g.drawImage(src, -x * xs, -y * ys, null);
                g.dispose();
            }
        }
        return res;
    }

}
